/*
 * @Description: operationHooks
 * @Author: qingyang
 * @Date: 2023-05-30 15:21:53
 * @LastEditors: qingyang
 * @LastEditTime: 2023-07-07 18:27:38
 */
import { reactive, markRaw } from 'vue'
import { removeUndefinedParams } from '@/utils/index'
import type { GetLogListParams } from '@/apis/system/systemLog/model/logListModel'
import type { BasicPagingListParams } from '@/apis/basicModel'
import { getLogList } from '@/apis/system/systemLog/logList'
import type { SearchOptionsType, CPageTableProps } from '@/components/CPageTable'

export const useLogList = () => {
  const searchForm = reactive<GetLogListParams>({
    operator: null,
    email: null,
    operateTimeBegin: null,
    operateTimeEnd: null,
    createTime: []
  })
  const searchOptions: SearchOptionsType[] = markRaw([
    {
      name: '操作人',
      field: 'operator',
      type: 'input'
    },
    {
      name: '登录邮箱',
      field: 'email',
      type: 'input'
    },
    {
      name: '操作日期',
      field: 'createTime',
      type: 'datePicker',
      startPlaceholder: '操作时间开始',
      endPlaceholder: '操作时间截止',
      dateType: 'daterange',
      format: 'YYYY-MM-DD'
    }
  ])
  const tableOptions: CPageTableProps['tableOptions'] = {
    loadData
  }
  function loadData(parameter: BasicPagingListParams) {
    const params: GetLogListParams & BasicPagingListParams = Object.assign(
      handleParams(),
      parameter
    )
    return getLogList(params)
  }
  const handleParams = (): GetLogListParams => {
    let params: GetLogListParams = {
      operator: searchForm.operator,
      email: searchForm.email,
      operateTimeBegin: searchForm.createTime?.length ? searchForm.createTime[0] : undefined,
      operateTimeEnd: searchForm.createTime?.length ? searchForm.createTime[1] : undefined
    }
    params = removeUndefinedParams(params)
    return params
  }

  const tableFieldsSetting: CPageTableProps['tableFieldsSetting'] = [
    {
      title: '操作人',
      field: 'operator',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '角色',
      field: 'role',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '登录邮箱',
      field: 'email',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作模块',
      field: 'operateModule',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '内容',
      field: 'content',
      columnSettingConfig: {
        enabled: true
      }
    },
    {
      title: '操作时间',
      field: 'createTime',
      columnSettingConfig: {
        enabled: true
      }
    }
  ]

  return {
    searchOptions,
    searchForm,
    tableOptions,
    tableFieldsSetting
  }
}
